/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.tb_supplier.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.tb_loading_address.entity.TbLoadingAddress;
import com.thinkgem.jeesite.modules.tb_supplier.entity.TbSupplier;
import com.thinkgem.jeesite.modules.tb_supplier.service.TbSupplierService;

/**
 * 供货商管理Controller
 * @author LiJie
 * @version 2017-06-09
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_supplier/tbSupplier")
public class TbSupplierController extends BaseController {

	@Autowired
	private TbSupplierService tbSupplierService;
	/**
	 * 查询单条信息
	 * @param String
	 * @param RedirectAttributes
	 * @return ArrayList<TbSupplier>
	 */
	@ModelAttribute
	public TbSupplier get(@RequestParam(required=false) String id) {
		TbSupplier entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbSupplierService.get(id);
		}
		if (entity == null){
			entity = new TbSupplier();
		}
		return entity;
	}
	/**
	 * 查询分页数据
	 * @param TbSupplier
	 * @param RedirectAttributes
	 * @param HttpServletRequest
	 * @param HttpServletResponse
	 * @param Model
	 * @return String
	 */
	@RequiresPermissions("tb_supplier:tbSupplier:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbSupplier tbSupplier, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbSupplier> page = tbSupplierService.findPage(new Page<TbSupplier>(request, response), tbSupplier); 
		model.addAttribute("page", page);
		return "modules/tb_supplier/tbSupplierList";
	}

	@RequiresPermissions("tb_supplier:tbSupplier:view")
	@RequestMapping(value = "form")
	public String form(TbSupplier tbSupplier, Model model) {
		model.addAttribute("tbSupplier", tbSupplier);
		return "modules/tb_supplier/tbSupplierForm";
	}
	/**
	 * 修改/添加供货商
	 * @param TbSupplier
	 * @param RedirectAttributes
	 * @return ArrayList<TbSupplier>
	 */
	@RequiresPermissions("tb_supplier:tbSupplier:edit")
	@RequestMapping(value = "save")
	public String save(TbSupplier tbSupplier, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbSupplier)){
			return form(tbSupplier, model);
		}
		tbSupplierService.save(tbSupplier);
		addMessage(redirectAttributes, "保存供货商成功");
		return "redirect:"+Global.getAdminPath()+"/tb_supplier/tbSupplier/?repage";
	}
	/**
	 * 删除供货商
	 * @param TbSupplier
	 * @param RedirectAttributes
	 * @return ArrayList<TbSupplier>
	 */
	@RequiresPermissions("tb_supplier:tbSupplier:edit")
	@RequestMapping(value = "delete")
	public String delete(TbSupplier tbSupplier, RedirectAttributes redirectAttributes) {
		tbSupplierService.delete(tbSupplier);
		addMessage(redirectAttributes, "删除供货商成功");
		return "redirect:"+Global.getAdminPath()+"/tb_supplier/tbSupplier/?repage";
	}
	/**
	 * 查看供货商详情
	 * @param String
	 * @return ArrayList<TbSupplier>
	 */
	@RequestMapping("SupplierDetails")
	@ResponseBody
	public ArrayList<TbSupplier> SupplierDetails(TbSupplier tbSupplier, HttpServletRequest request, HttpServletResponse response, Model model){
		String id = request.getParameter("id");
		ArrayList<TbSupplier> TbSupplier = tbSupplierService.supplierDetails(id);
		return TbSupplier;
	}
	
}
